package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.utils.MinIoUtil;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    public PageResult pageSearch(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.DishQuery(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishVO> list = page.getResult();

        return new PageResult(total, list);
    }

    public DishVO getDishById(Integer id) {
        return dishMapper.getDishById(id);
    }

    @Transactional
    public void addDishWithFlavor(DishDTO dishDTO) throws Exception {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.addDishWithFlavor(dish);
        //获取insert生成的主键
        Long dishId = dish.getId();
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if (dishFlavors != null && dishFlavors.size() > 0) {
            dishFlavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishMapper.insertBatch(dishFlavors);
        }


    }


    public List<DishVO> categoryIdByList(Integer categoryId) {
        // 查询菜品列表
        List<DishVO> list = dishMapper.categoryIdByList(categoryId);
        log.info("list:{}", list);
        // 遍历列表，为每个菜品设置对应的口味列表
        for (DishVO dishVO : list) {
            Integer dishId = Math.toIntExact(dishVO.getId()); // 获取 dishId
            List<DishFlavor> dishFlavors = getDishFlavorsByDishId(dishId); // 查询口味列表
            log.info("dishFlavors的值为:{}", dishFlavors);
            dishVO.setFlavors(dishFlavors); // 将口味列表设置到 DishVO 对象中
        }

        return list;
    }

    public void statusDish(Integer status, Long id) {
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.updateDish(dish);
    }




    @Transactional(rollbackFor = Exception.class)
    public void updateDishWithFlavors(DishDTO dishDTO) {
        // 1. 更新菜品信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateDish(dish);

        // 更新菜品信息
        // 2. 获取菜品 ID
        Long dishId = dish.getId();

        List<Long> ids = new ArrayList<>();
        ids.add(dishId);

        dishMapper.deleteDishFlavorByIds(ids);

        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if (dishFlavors != null && dishFlavors.size() > 0) {
            dishFlavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishMapper.insertBatch(dishFlavors);
        }


    }


    @Transactional(rollbackFor = Exception.class)
    public void deleteDishes(List<Long> ids) {
        //是否起售
        Long Status = dishMapper.getDishAndCategoryById(ids);
        if (Status >= 1) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //是否关联套餐
        List<Long> Setmeal = setmealMapper.getSetmealAndDishById(ids);
        if (Setmeal != null && Setmeal.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        dishMapper.deleteDishesByIds(ids);
        deleteDishFlavorByIds(ids);
    }


    public void deleteDishFlavorByIds(List<Long> ids) {
        dishMapper.deleteDishFlavorByIds(ids);
    }


    public Long getSetmealAndDishById(List<Long> ids) {
        return dishMapper.getSetmealAndDishById(ids);
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param id
     * @return
     */
    public List<DishItemVO> getSetmealByDishId(Integer id) {
        List<DishItemVO> setmealByDishId = dishMapper.getSetmealByDishId(id);
        return setmealByDishId;
    }

    public List getSetmealByCategoryId(List categoryId){
        return dishMapper.getSetmealByCategoryId(categoryId);
    };

    /**
     * 根据dishId查询菜品口味
     * @param dishId
     * @return
     */
    public  List<DishFlavor> getDishFlavorsByDishId(Integer dishId) {
        return dishMapper.getDishFlavorsByDishId(dishId);
    }

}
